/* 
    Project     : 48c6c450f1a4a0cc53d9585dc0fee742
    Created on  : Mar 16, 2013, 11:29:15 PM
    Author      : Truong Khuong - khuongxuantruong@gmail.com
    Description :
        Purpose of the stylesheet follows.
*/
var CAT_CONGIF={
    JQXGRID_ELEMENT     :   '#jqxGrid_Backend_Categories',
    CATE_TYPE           :   'kitchen',
    BINDING_URI         :   base_url+'backend/category/binding/',
    EDIT_PANEL_URI      :   base_url+"backend/category/EditPanel",
    COMMIT_URI          :   base_url+"backend/excution/commit/categories/categories/cate",
    DELETE_URI          :   base_url+"backend/excution/delete/categories/categories/cate",
    PENDING             : false
};
var Categories=(function(){
    var init=false;
    var datafields=[
        { name  :   'cate_id' ,type :   'int'},
        { name  :   'cate_type' },
        { name  :   'cate_title' },
        { name  :   'cate_display_title' },
        { name  :   'cate_alias' },
        { name  :   'cate_parent_title' },
        { name  :   'cate_position',type    :   'int' },
        { name  :   'cate_link' },
        { name  :   'cate_thumb' },
        { name  :   'cate_status', type : 'bool' },
        { name  :   'cate_insert' },
        { name  :   'cate_update' },
        { name  :   'cate_value' },
        { name  :   'cate_target' },
        { name  :   'cate_desc' }
    ],
    columns=[
        { 
            text: 'Thumb'     , dataField: 'cate_thumb'       ,width:60,filterable:false ,sortable: false,
            cellsrenderer : function (row, datafield, value) {
                if(value==undefined || value=='')
                    return '<img class="cellthumb" src="'+base_url+'libraries/images/16/picture.png"/>';
                else{
                    
                    return '<img onError="this.src = \''+value+'\'" class="cellthumb" height="32" src="' + getImageSrc(value) + '"/>';
                }
            }
        },
        { text: 'Title'     , dataField: 'cate_display_title'       ,filterable:false },
        { text: 'Parent'    , dataField: 'cate_parent_title'     ,width:100,filterable:false },
        { text: 'Position'  , dataField: 'cate_position'    ,width:80,filterable:false, cellsalign:'right',columntype:'numberinput',filtertype:'number'},
        { 
            text: 'Type'      , dataField: 'cate_type'        ,width:100,filterable:false ,sortable: false,
            hidden:true,
            cellsrenderer : function (row, datafield, value) {
                for(c in category_type_value)
                    if(category_type_value[c]==value)
                        return '<span class="m-t-4 m-l-4">' + category_type_display[c] + '</span>';
                return '<span class="m-t-4 m-l-4">' + value + '</span>';
                 
            }
        },
        { text: 'Status'    , dataField: 'cate_status'      ,width:80,filterable:false ,columntype:'checkbox',threestatecheckbox:false,filtertype:'bool'},
        { text: 'Insert'    , dataField: 'cate_insert'      ,width:120,filterable:false ,cellsformat:'yyyy-MM-dd HH:mm:ss'},
        { 
            text: '#'        , dataField: 'cate_id'          ,width:60,filterable:false ,sortable: false,
            cellsrenderer : function (row, column, value) {
                var str="<span style='margin: 6px; float: left;'>";
                if(value && value>0){
                    try{
                        //value = $.parseJSON(value);
                        str+="\
                            <span onclick=\"Categories.EditItem('"+value+"');\" \
                             title='Edit'><img src=\"/libraries/images/16/edit_black.png\"/></span>\
                        ";
                        str+="\
                            <span onclick=\"Categories.DeleteItem('"+value+"');\" \
                             title='Delete'><img src=\"/libraries/images/16/delete_black.png\"/></span>\
                        ";
                    }catch(e){$.jGrowl(e.message,{ sticky: true, theme: 'growl-error', header: 'Error!' });}
                }
                str+="</span>";
                return str;
            }
        },
    ],
    source,dataAdapter,
    grid_element=CAT_CONGIF.JQXGRID_ELEMENT;
    return {
        OnInit:function(){
            try{
                if(init) return;
                init=true;
                //grid_element=$('#jqxGrid_Backend_Categories');
                source ={
                    datatype: "json",type : "POST",
                    datafields: datafields,
                    id: 'cate_id',
                    url: CAT_CONGIF.BINDING_URI,
                    filter      : function(){ $(CAT_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata', 'filter'); },
                    sort        : function(){ $(CAT_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata'); },
                    root        : 'rows',
                    beforeprocessing: function(data){ source.totalrecords = data.total_rows; }
                };
                dataAdapter = new $.jqx.dataAdapter(source, {
                    loadError: function(xhr, status, error){
                        $.jGrowl("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+error+"<br/>",{ sticky: true, theme: 'growl-error', header: 'Error!' });
                    }
                });
                $(CAT_CONGIF.JQXGRID_ELEMENT).jqxGrid({
                    rendergridrows: function(obj){
                        return obj.data;    
                    },ready: function () {
                        Categories.Filter('cate_type',CAT_CONGIF.CATE_TYPE);
                        
                    },
                    width: '100%',//autoheight:true,
                    source: dataAdapter,
                    theme: theme,sortable: true,
                    //filterable: true,
                    //autoshowfiltericon: true,
                    //showfilterrow: true,
                    rowsheight:34,
                    showemptyrow: true,
                    virtualmode:false,
                    columns: columns
                });
                
            }catch(e){
                $.jGrowl(e.message,{ sticky: true, theme: 'growl-error', header: 'Error!' });
            }
        },
        AddItem:function(){
            this.EditItem(0);
        },
        EditItem:function(Id){
            if(CAT_CONGIF.PENDING) return false;
            bootbox.dialog({
                backdrop:false,
                message: "Loading...",
                title: "Category",
                buttons: {
                    save: {
                        label: "Save",
                        className: "btn-success",
                        callback: function() {
                            return Categories.Save();
                        }
                    },
                    cancel: {
                        label: "Cancel",
                        className: "btn-danger",
                        callback: function() {
                            Example.show("Cancel !");
                        }
                    }
                }
            });
            CAT_CONGIF.PENDING=true;
            new backend({
                url         :   CAT_CONGIF.EDIT_PANEL_URI,
                data        :   {
                    Id      :   Id,
                    Type    :   CAT_CONGIF.CATE_TYPE
                },
                callback    :   function(rsdata){
                    CAT_CONGIF.PENDING=false;
                    if(rsdata.result<0){
                        bootbox.setHtml(rsdata.message);
                    }else{
                        bootbox.setHtml(rsdata.message);
                        $(".collapse").collapse()
                        $('#category_edit_panel_tabs').jqxTabs({
                            theme: 'metro', height: 'auto',  scrollable: true, 
                            selectionTracker: true, animationType: 'fade'
                        });
                        $('#cate_parent').selectpicker({cls:'btn-sm'});
                    }
                }
            }).call();
        },
        Save:function(){
            if(CAT_CONGIF.PENDING) return false;
            //if( $('#category_form_edit_panel').validationEngine('validate') === false)return false;
            var Params=$('#category_form_edit_panel').serializeObject();
            Params.cate_type=CAT_CONGIF.CATE_TYPE;
            CAT_CONGIF.PENDING=true;
            new backend({
                url         :   CAT_CONGIF.COMMIT_URI,
                data        :   {
                    Params  :   Params,
                    Type    :   CAT_CONGIF.CATE_TYPE,
                    Id      :   $('#cate_id').val()
                },
                callback    :   function(rsdata){
                    CAT_CONGIF.PENDING=false;
                    if(rsdata.result<0){
                        $.jGrowl(rsdata.message,{ sticky: true, theme: 'growl-error', header: 'Error!' });
                        return false;
                    }else{
                        $.jGrowl(rsdata.message);
                        Categories.Cancel();
                        Categories.Refresh();
                        return true;
                    }
                }
            }).call();
            return false;
        },
        Cancel:function(){
            $('#category_form_edit_panel').parents('.bootbox').hide().remove();
        },
        DeleteItem:function(id){
            bootbox.confirm("These items will be permanently deleted and cannot be recovered. Are you sure ?", function(result) {
                if(!result) return;
                backend_delete('categories','categories','cate',id,function(){
                    
                });
            });
        },
        RestoreItem:function(){
            accessdenied();
        },
        Permission:function(id){
            
        },
        PrivateItem:function(){
            accessdenied();
        },
        Setting:function(){
            bckdialog({
                message:$('#window-sOpt'),
                icon:"<img class='p-a t-7 l-8' src='"+base_url+"/libraries/images/16/option.gif'/>",
                title:'Setting'
            }).open();
        },
        Refresh:function(){
            //$(CAT_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata');
            //Msg('✔ Dép bông nhung mềm mịn - 68.000 | Bộ màu vẽ 68 món - 85.000 | Máy uốn tóc new - 135.000');
        },
        Filter:function(datafield,filtervalue){
            CAT_CONGIF.CATE_TYPE=filtervalue;
            var filtertype = 'stringfilter';
            var filtergroup = new $.jqx.filter();
            var filter_or_operator = 1;
            var filtercondition = 'equal';
            var filter = filtergroup.createfilter(filtertype, filtervalue, filtercondition);
            filtergroup.addfilter(filter_or_operator, filter);
            
            //$(grid_element).jqxGrid('clearfilters');
            // add the filters.
            $(CAT_CONGIF.JQXGRID_ELEMENT).jqxGrid('addfilter', datafield, filtergroup);
            // apply the filters.
            $(CAT_CONGIF.JQXGRID_ELEMENT).jqxGrid('applyfilters');
        }
    };
}());
$(document).ready(function () {
    //Btree().Init();
});